/**
 * 流程历史
 */
layui.define(['index', 'flying'], function (exports) {  //先加载index模块，里面配置了全局的入口
  const admin = layui.admin,
      flying = layui.flying
      ,form = layui.form
      ,table = layui.table
      ,layer = layui.layer
      ,dropdown = layui.dropdown;

  flying.table({
    elem: '#LAY-history-table',
    url: '/flow/history/page', //数据接口
    cols: [
      {type: 'numbers', fixed: 'left', title: '序号'}
      , {field: 'bizKey', title: '业务流水号', width: 190}
      , {field: 'procDefName', title: '流程名称', minWidth: 150}
      , {field: 'startTime', title: '开始时间', templet: '<div>{{ layui.util.toDateString(d.startTime, "yyyy-MM-dd HH:mm:ss") }}</div>',  minWidth: 170, sort: true}
      , {field: 'endTime', title: '结束时间', templet: '<div>{{ layui.util.toDateString(d.endTime, "yyyy-MM-dd HH:mm:ss") }}</div>',  minWidth: 170, sort: true}
      , {field: 'duration', title: '持续时间', templet: function (d) {
          return flying.formatDuration(d.duration);
        }, width: 180, hide: true}
      , {field: 'startUserId', title: '流程发起人', width: 150}
      , {field: 'nodeName', title: '当前节点', minWidth: 120}
      , {field: 'assignee', title: '当前处理人', width: 150}
      , {field: 'businessStatus', title: '流程状态', width: 100, templet: function(d){
          if(d.businessStatus === 'success'){
            return '<span class="layui-badge layui-bg-blue">审批通过</span>';
          } else if(d.businessStatus === 'refuse'){
            return '<span class="layui-badge">审批拒绝</span>';
          } else if(d.businessStatus === 'running'){
            return '<span class="layui-badge layui-bg-green">正在审批</span>';
          } else if(d.businessStatus === 'reject'){
            return '<span class="layui-badge">审批驳回</span>';
          } else if(d.businessStatus === 'waiting'){
            return '<span class="layui-badge layui-bg-gray">等待审批</span>';
          } else if(d.businessStatus === 'cancel'){
            return '<span class="layui-badge layui-bg-gray">流程撤回</span>';
          } else {
            return '<span class="layui-badge layui-bg-cyan">流程作废</span>';
          }
        }, unresize: true}
      , {field: 'deleteReason', title: '删除原因', minWidth: 110}
      , {field: 'procInstId', title: '流程实例ID', minWidth: 130, hide: true}
      , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#history-table'}
    ]
  });

  const history = {
    // 删除（作废）
    handleDelete: function (index, procInstId) {
      //提交 Ajax 成功后，更新表格中的数据
      admin.req({
        url: '/flow/instance/delete'
        , type: 'post'
        , data: {"procInstId": procInstId, "reason": '作废删除'}
        , done: function (res) {
          //执行成功
          layer.msg(res.msg, {icon: 1});
          table.reload('LAY-history-table'); //数据刷新
          layer.close(index); //关闭弹层
        }
      });
    },
    // 撤回
    handleCancel: function (procInstId) {
      //提交 Ajax 成功后，更新表格中的数据
      admin.req({
        url: '/flow/instance/cancel'
        , type: 'post'
        , data: {"procInstId": procInstId}
        , done: function (res) {
          //执行成功
          layer.msg(res.msg, {icon: 1});
          table.reload('LAY-history-table'); //数据刷新
        }
      });
    },
    // 委派任务
    handleDelegate: function (taskId, assignee) {
      //提交 Ajax 成功后，更新表格中的数据
      admin.req({
        url: '/flow/instance/task/delegate'
        , type: 'post'
        , data: {"taskId": taskId, "assignee": assignee}
        , done: function (res) {
          //执行成功
          layer.msg(res.msg, {icon: 1});
          table.reload('LAY-history-table'); //数据刷新
        }
      });
    },
    // 指派任务
    handleAssign: function (taskId, assignee) {
      //提交 Ajax 成功后，更新表格中的数据
      admin.req({
        url: '/flow/instance/task/assign'
        , type: 'post'
        , data: {"taskId": taskId, "assignee": assignee}
        , done: function (res) {
          //执行成功
          layer.msg(res.msg, {icon: 1});
          table.reload('LAY-history-table'); //数据刷新
        }
      });
    }
  }

  //对外暴露的接口
  exports('history', history);
});